package com.example.lottery.controller;

import com.example.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.example.lottery.common.exception.ControllerException;
import com.example.lottery.common.pojo.CommonResult;
import com.example.lottery.controller.param.DrawPrizeParam;
import com.example.lottery.controller.param.WinningRecordParam;
import com.example.lottery.controller.result.WinningRecordResult;
import com.example.lottery.service.IDrawPrizeService;
import com.example.lottery.service.dto.WinningRecordDTO;
import com.example.lottery.service.impl.DrawPrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class DrawPrizeController {
    @Autowired
    private IDrawPrizeService drawPrizeService;
    @PostMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Validated @RequestBody DrawPrizeParam drawPrizeParam){
        log.info("drawPrize DrawPrizeParam:{}",drawPrizeParam);
        //  消息发送给MQ
        drawPrizeService.draw(drawPrizeParam);
        //直接返回true 保证时效性 显示中奖结果
        return CommonResult.success(true);
    }

    /**
     * 显示中奖记录
     * @param winningRecordParam
     * @return
     */
    @PostMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(@RequestBody @Validated WinningRecordParam winningRecordParam){
        log.info("showWinningRecords WinningRecordParam:{}",winningRecordParam);
        List<WinningRecordDTO> winningRecordDTOS=drawPrizeService.showWinningRecords(winningRecordParam);
        return CommonResult.success(convertToWinningRecordResultList(winningRecordDTOS));
    }

    private List<WinningRecordResult> convertToWinningRecordResultList(List<WinningRecordDTO> winningRecordDTOS) {
        if(winningRecordDTOS.isEmpty()){
            log.info("显示中奖记录失败");
            throw new ControllerException(ControllerErrorCodeConstants.DRAW_PRIZE_SHOW_RECORDS_ERROR);
        }
        List<WinningRecordResult> winningRecords = winningRecordDTOS.stream().map(winningRecordDTO -> {
            WinningRecordResult winningRecordResult = new WinningRecordResult();
            winningRecordResult.setWinnerId(winningRecordDTO.getWinnerId());
            winningRecordResult.setWinnerName(winningRecordDTO.getWinnerName());
            winningRecordResult.setPrizeName(winningRecordDTO.getPrizeName());
            winningRecordResult.setPrizeTier(winningRecordDTO.getPrizeTier().getMsg());
            winningRecordResult.setWinningTime(winningRecordDTO.getWinningTime());
            return winningRecordResult;
        }).collect(Collectors.toList());
        return winningRecords;
    }
}
